
package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.util.DataFileUtil;
import com.fdb.efp.nls.service.dao.BatBasmDao;
import com.fdb.efp.nls.service.domain.BatBasm;
import com.fdb.efp.nls.service.facade.BatBasmService;
import com.fdb.efp.nls.service.vo.BatBasmVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * ServiceImpl: 利率配置中间表
 * <p>
 * 
 * @author fuzm
 * @date 2018-06-04
 */
@Service("batBasmService")
public class BatBasmServiceImpl implements BatBasmService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(BatBasmServiceImpl.class);

	@Autowired
	private BatBasmDao batBasmDao;

	public static final int batchInsertNum = 100;

	/**
	 * 新增操作
	 */
	@Override
	public int insertBatBasm(BatBasmVO inBatBasmVo) {
		logger.debug("当前新增数据为:" + inBatBasmVo.toString());
		int num = 0;
		try {
			BatBasm batBasm = new BatBasm();
			beanCopy(inBatBasmVo, batBasm);
			num = batBasmDao.insertBatBasm(batBasm);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(BatBasmVO inBatBasmVo) {
		logger.debug("当前删除数据条件为:" + inBatBasmVo);
		int num = 0;
		try {
			BatBasm batBasm = new BatBasm();
			beanCopy(inBatBasmVo, batBasm);
			num = batBasmDao.deleteByPk(batBasm);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inBatBasmVo + "删除的数据条数为" + num);
		return num;
	}

	@Override
	public int deleteAll() {
		int num = 0;
		try {
			num = batBasmDao.deleteAll();
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(BatBasmVO inBatBasmVo) {
		logger.debug("当前修改数据为:" + inBatBasmVo.toString());
		int num = 0;
		try {
			BatBasm batBasm = new BatBasm();
			beanCopy(inBatBasmVo, batBasm);
			num = batBasmDao.updateByPk(batBasm);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inBatBasmVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	@Override
	public BatBasmVO queryByPk(BatBasmVO inBatBasmVo) {

		logger.debug("当前查询参数信息为:" + inBatBasmVo);
		try {
			BatBasm queryBatBasm = new BatBasm();
			beanCopy(inBatBasmVo, queryBatBasm);
			BatBasm queryRslBatBasm = batBasmDao.queryByPk(queryBatBasm);
			if (Objects.nonNull(queryRslBatBasm)) {
				BatBasmVO outBatBasmVo = beanCopy(queryRslBatBasm, new BatBasmVO());
				logger.debug("当前查询结果为:" + outBatBasmVo.toString());
				return outBatBasmVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<BatBasmVO> queryAllOwner(BatBasmVO batBasmVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BatBasmVO> list = null;
		try {
			List<BatBasm> batBasms = batBasmDao.queryAllOwnerByPage(batBasmVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + batBasms.size());
			pageSet(batBasms, batBasmVo);
			list = (List<BatBasmVO>) beansCopy(batBasms, BatBasmVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<BatBasmVO> queryAllCurrOrg(BatBasmVO batBasmVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BatBasm> batBasms = batBasmDao.queryAllCurrOrgByPage(batBasmVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + batBasms.size());
		List<BatBasmVO> list = null;
		try {
			pageSet(batBasms, batBasmVo);
			list = (List<BatBasmVO>) beansCopy(batBasms, BatBasmVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<BatBasmVO> queryAllCurrDownOrg(BatBasmVO batBasmVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BatBasm> batBasms = batBasmDao.queryAllCurrDownOrgByPage(batBasmVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + batBasms.size());
		List<BatBasmVO> list = null;
		try {
			pageSet(batBasms, batBasmVo);
			list = (List<BatBasmVO>) beansCopy(batBasms, BatBasmVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	@Override
	public void fileDataInsertTable(File file, BatBasmVO clazz) {
		try {
			List<BatBasmVO> readTxt = DataFileUtil.readTxt(file, clazz.getClass(), "batBasm.json");
			if (readTxt != null && readTxt.size() > 0) {
				int sum = readTxt.size();
				logger.info("文件中总数据量为:" + sum);
				logger.info("分批处理设置每次处理条数:" + batchInsertNum);
				int cycleNum = sum % batchInsertNum == 0 ? sum / batchInsertNum : sum / batchInsertNum + 1;
				logger.info("需要执行的处理次数:" + cycleNum);
				int insertNum = 0;
				for (int i = 0; i < cycleNum; i++) {
					logger.info("开始执行第" + (i + 1) + "次处理");
					int start = i * batchInsertNum;
					int end = start + batchInsertNum;
					logger.info("查询数据范围:" + (start + 1) + "~" + end);
					List<BatBasmVO> subList = new ArrayList<BatBasmVO>();
					if (end > readTxt.size()) {
						subList = readTxt.subList(start, readTxt.size());
					} else {
						subList = readTxt.subList(start, end);
					}
					for (BatBasmVO vo : subList) {
						BatBasm batComDtl = new BatBasm();
						beanCopy(vo, batComDtl);
						int num = batBasmDao.insertBatBasm(batComDtl);
						insertNum = insertNum + num;
					}
				}
				logger.info("文件中数据成功插入数量为:" + insertNum);
			}
		} catch (Exception e) {
			logger.error("文件数据插入表过程出错", e);
		}
	}

}
